all files / components/ SDDropdown.react.js

79.31% Statements 69/87
72.34% Branches 34/47
65% Functions 13/20
57.89% Lines 22/38
4 statements, 1 function, 11 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 2071× 1×   1× 1× 1× 1× 1×     1×                                                                                                                                                   1×                         1×     1×                   1×       1× 1×                                     1×     1×   1×               1×                                                                               3×                               1× 1×    
import React from 'react';
import PropTypes from 'prop-types';
 
import Dialog from 'material-ui/Dialog';
import DropDownMenu from 'material-ui/DropDownMenu';
import FlatButton from 'material-ui/FlatButton';
import MenuItem from 'material-ui/MenuItem';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
 
 
const propTypes = {
  id: PropTypes.string,
 
  /**
   * An array of options
   */
  options: PropTypes.arrayOf(
    PropTypes.shape({
      /**
       * The checkbox's label
       */
      label: PropTypes.string,
 
      /**
       * The value of the checkbox. This value
       * corresponds to the items specified in the
       * `values` property.
       */
      value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
 
      /**
       * If true, this checkbox is disabled and can't be clicked on.
       */
      disabled: PropTypes.bool
    })
  ).isRequired,
 
  /**
   * The value of the input. If `multi` is false (the default)
   * then value is just a string that corresponds to the values
   * provided in the `options` property. If `multi` is true, then
   * multiple values can be selected at once, and `value` is an
   * array of items with values corresponding to those in the
   * `options` prop.
   */
  value: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.arrayOf(PropTypes.string)
  ]),
 
  /**
   * className of the dropdown element
   */
  className: PropTypes.string,
 
  /**
   * className of the button element
   */
  buttonClassName: PropTypes.string,
 
  /**
   * Description for the variable
   */
  description: PropTypes.string,
 
  /**
   * Human name for the variable
   */
  humanName: PropTypes.string,
 
  /** Text to display when all values are selected, e.g. 'all ages' */
  allValuesText: PropTypes.string,
 
  /**
   * If true, the user can select multiple values
   */
  multi: PropTypes.bool,
 
  /**
   * Dash-assigned callback that gets fired when the input changes
   */
  setProps: PropTypes.func
};
 
const defaultProps = {
  multi: false
};
 
 
/**
 * SDDropdown is an example component.
 * It takes a property, `label`, and
 * displays it.
 * It renders an input with the property `value`
 * which is editable by the user.
 */
export default class SDDropdown extends React.Component {
  constructor(props) {
    super(props);
 
    this.state = {
      value: props.value,
      editorOpen: false
    };
  }
 
  componentWillReceiveProps(newProps) {
    this.setState({value: newProps.value});
  }
 
  get compressedLabel() {
    const { options, multi, allValuesText } = this.props;
    const { value } = this.state;
 
    Eif (typeof value === 'undefined')
      return 'None selected';
 
    let selectedLabels = multi
      ? options.filter(option => value.indexOf(option.value) > -1)
      : options.filter(option => option.value === value);
    selectedLabels = selectedLabels.map(option => option.label);
 
    switch (selectedLabels.length) {
      case 0:
        return 'None selected';
      case 1:
        return selectedLabels[0];
      case options.length:
        return typeof allValuesText === 'undefined' ? 'All selected' : allValuesText;
      default:
        return `${selectedLabels.slice(0, -1).join(', ')} or ${selectedLabels[selectedLabels.length - 1]}`;
    }
  }
 
  render() {
    const { id, options, humanName, buttonClassName, description, setProps, multi } = this.props;
 
    const compressedLabel = this.compressedLabel;
 
    const actions = [
      <FlatButton
        label="Done editing selection"
        primary={true}
        onClick={() => { this.setState({editorOpen: false}); }}
      />
    ];
 
    return (
      <MuiThemeProvider>
        <div id={id} className="sd-dropdown">
          <div className="compressed-label">
            <a
              href="#edit-dropdown"
              onClick={(event) => {
                event.preventDefault();
                this.setState({editorOpen: true});
              }}
              className="edit"
            >
              <span className="label-text">{compressedLabel}</span>
              <span className={buttonClassName} />
              <span className="edit-rarr">&rarr;</span>
            </a>
          </div>
 
          <Dialog
            title={typeof humanName === 'string'
              ? <h6>Edit options for <strong>{humanName}</strong></h6>
              : <h6>Edit options</h6>}
            actions={actions}
            modal={false}
            open={this.state.editorOpen}
            onRequestClose={() => { this.setState({editorOpen: false}); }}
          >
            <div>
              <p>{description}</p>
              <p>Selected: <strong>{compressedLabel}</strong></p>
              <DropDownMenu
                value={this.state.value}
                onChange={(event, index, value) => {
                  this.setState({value});
                  if (setProps) setProps({value});
                }}
                multiple={multi}
                style={{maxWidth: 400}}
              >
                {
                  options.map(option => (
                    <MenuItem
                      key={option.value}
                      primaryText={option.label}
                      value={option.value}
                      disabled={option.disabled}
                    />))
                }
              </DropDownMenu>
            </div>
          </Dialog>
        </div>
      </MuiThemeProvider>);
  }
}
 
SDDropdown.propTypes = propTypes;
SDDropdown.defaultProps = defaultProps;